home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / demostuf / rubber2.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-07-25  |  9.2 KB  |  573 lines

  1. program rubbervector1;
  2. {
  3.     RubberVector #1
  4.     - by Bjarke Viksoe
  5.     16/2/1994
  6.  
  7.   THIS PROGRAM WAS CODED BY BJARKE VIKS0E.
  8.   YOU ARE FREE TO DO WHATEVER YOU WANT WITH THIS PIECE OF CODE.
  9.   E-MAIL ME AT: dat92230@rix02.lyngbyes.dk IN 1994 FOR CHAT AND CODE.
  10.  
  11.     - must run in protected mode to have enough memory...
  12. }
  13.  
  14. uses
  15.     DEMOINIT;
  16.  
  17. const
  18.     DEBUG = FALSE;
  19.     ANTAL_FACES = 6;
  20.     ANTAL_COORDS = 8;
  21.  
  22.     box = 89;
  23.     ANIMWIDTH = 40;
  24.     ANIMHEIGHT = 100;
  25.     ANTAL_ANIMS = ANIMHEIGHT;
  26.  
  27. type
  28.     pAnim = ^animtype;
  29.     animtype = array[0..ANIMWIDTH*ANIMHEIGHT*4] of byte;
  30.  
  31.     facetype = RECORD
  32.         l1,l2,l3,l4 : byte;
  33.     end;
  34.  
  35. var
  36.     slope                    : array[0..399] of integer;
  37.     face                    : array[1..ANTAL_FACES] of facetype;
  38.     light                    : array[1..ANTAL_FACES] of byte;
  39.     cbuffer                : array[0..ANTAL_COORDS*2-1] of integer;
  40.     miny,maxy             : integer;
  41.  
  42.     i : integer;
  43.     xkoord,ykoord,zkoord : integer;
  44.  
  45.     sinustabel            : array[0..1279] of integer;
  46.     v1,v2,v3                : word;
  47.     cos1,sin1,cos2,sin2,cos3,sin3 : integer;
  48.  
  49.     animpos : integer;
  50.     anim : array[0..ANTAL_ANIMS] of pAnim;
  51.     animytabel : array[0..200] of word;
  52.  
  53.  
  54. const
  55.     display1 : integer = $0000;
  56.     display2 : integer = $4000;
  57.     coords : array[0..ANTAL_COORDS*3-1] of integer =
  58.         (box,box,-box, -box,box,-box, -box,-box,-box, box,-box,-box,
  59.         box,box,box, -box,box,box, -box,-box,box, box,-box,box);
  60.  
  61.  
  62. (*------------------------------------------------*)
  63.  
  64. procedure SetupSinus;
  65. var
  66.     i : integer;
  67.     v, vadd : real;
  68. begin
  69.     v:=0.0;
  70.     vadd:=(2.0*pi/1024.0);
  71.     for i:=0 to 1279 do begin
  72.         sinustabel[i]:=round(sin(v)*32767);
  73.         v:=v+vadd;
  74.     end;
  75. end;
  76.  
  77. procedure SetupCoords;
  78. begin
  79.     with face[1] do begin l1:=3; l2:=2; l3:=1; l4:=0; end;
  80.     with face[2] do begin l1:=4; l2:=5; l3:=6; l4:=7; end;
  81.     with face[3] do begin l1:=0; l2:=1; l3:=5; l4:=4; end;
  82.     with face[4] do begin l1:=1; l2:=2; l3:=6; l4:=5; end;
  83.     with face[5] do begin l1:=2; l2:=3; l3:=7; l4:=6; end;
  84.     with face[6] do begin l1:=3; l2:=0; l3:=4; l4:=7; end;
  85. end;
  86.  
  87. procedure SetupColors;
  88. var
  89.     i : integer;
  90. begin
  91.     for i:=0 to 63 do setRGB(i, 0,i,0);
  92.     for i:=64 to 127 do setRGB(i, 0,127-i,0);
  93.     for i:=128 to 192 do setRGB(i, 0,i-128,0);
  94.     setRGB(0, 2,4,8);
  95. end;
  96.  
  97. procedure InitDemo;
  98. var
  99.     i : integer;
  100. begin
  101.     ClearWholeScreen;
  102.  
  103.     SetupSinus;
  104.     SetupColors;
  105.     SetupCoords;
  106.  
  107.     for i:=0 to ANTAL_ANIMS do begin
  108.         new(anim[i]);
  109.         fillchar(anim[i]^,ANIMWIDTH*ANIMHEIGHT*4,0);
  110.     end;
  111.     for i:=0 to 200 do animytabel[i]:=i*ANIMWIDTH;
  112.  
  113.     v1:=0; v2:=0; v3:=0;
  114.     animpos:=0;
  115. end;
  116.  
  117. procedure UnInitDemo;
  118. var
  119.     i : integer;
  120. begin
  121.     for i:=0 to ANTAL_ANIMS do dispose(anim[i]);
  122. end;
  123.  
  124.  
  125. (*------------------------------------------------*)
  126.  
  127. procedure SwapDisplay;
  128. var
  129.     temp : word;
  130. begin
  131.     temp:=display2;
  132.     display2:=display1;
  133.     display1:=temp;
  134.     SetAddress(Ptr(SEGA000,display1));
  135. end;
  136.  
  137. procedure ClearScreen(anim : pAnim); assembler;
  138. asm
  139.     les    di,anim
  140.     DB $66,$33,$c0        {xor eax,eax}
  141.     mov    cx,ANIMWIDTH*ANIMHEIGHT
  142.     cld
  143.     DB $F3,$66,$AB        {rep stosd}
  144. end;
  145.  
  146.  
  147. (*------------------------------------------------*)
  148.  
  149. procedure ClearSlope; assembler;
  150. asm
  151.     mov    ax,ds
  152.     mov    es,ax
  153.     lea    di,slope
  154.     DB $66,$B8,$00,$80,$00,$80        {MOV AX,$80008000}
  155.     cld
  156.     mov    cx,200
  157.     DB $F3,$66,$AB                        {REP STOSD}
  158. end;
  159.  
  160. procedure CalcSlope(l1,l2 : integer); assembler;
  161. var
  162.     ysize : integer;
  163. asm
  164.     lea    si,cbuffer
  165.     mov    bx,l1
  166.     shl    bx,2
  167.     mov    cx,[si+bx]
  168.     mov    dx,[si+bx+2]
  169.     mov    bx,l2
  170.     shl    bx,2
  171.     add    si,bx
  172.     mov    ax,[si]
  173.     mov    bx,[si+2]
  174.  
  175.     cmp    bx,dx
  176.     jle    @noswap
  177.     xchg    ax,cx
  178.     xchg    bx,dx
  179. @noswap:
  180.     cmp    bx,miny
  181.     jae    @miny
  182.     mov    miny,bx
  183. @miny:
  184.     cmp    dx,maxy
  185.     jbe    @maxy
  186.     mov    maxy,dx
  187. @maxy:
  188.  
  189.     sub    dx,bx
  190.     mov    ysize,dx
  191.     add    bx,bx
  192.     add    bx,bx
  193.     lea    si,slope
  194.     add    si,bx
  195.  
  196.     push    ax
  197.     sub    cx,ax
  198.     inc    cx
  199.  
  200.     and    dx,dx
  201.     jz        @zero
  202.     cmp    dl,1
  203.     jne    @not1
  204.     dec    cx
  205.     mov    dx,cx
  206.     xor    ax,ax
  207.     jmp    @one
  208. @not1:
  209.     cmp    dl,2
  210.     jne    @not2
  211.     mov    ax,$7FFF
  212.     imul    cx
  213.     jmp    @one
  214. @not2:
  215.  
  216.     mov    dx,$0001
  217.     mov    ax,$0000
  218.     idiv    ysize
  219.     imul    cx
  220. @one:
  221.     pop    cx
  222.     xor    bx,bx
  223.  
  224.     mov    di,$8000
  225. @loop:
  226.     cmp    [si],di
  227.     jne    @other
  228.     mov    [si],cx
  229.     add    si,4
  230.     add    bx,ax
  231.     adc    cx,dx
  232.     dec    ysize
  233.     jnz    @loop
  234.     jmp    @zero
  235. @other:
  236.     mov    [si+2],cx
  237.     add    si,4
  238.     add    bx,ax
  239.     adc    cx,dx
  240.     dec    ysize
  241.     jnz    @loop
  242. @zero:
  243. end;
  244.  
  245.  
  246. (*------------------------------------------------*)
  247.  
  248. procedure CalcVinkel;
  249. begin
  250.     sin1:=sinustabel[v1];
  251.     cos1:=sinustabel[v1+256];
  252.     sin2:=sinustabel[v2];
  253.     cos2:=sinustabel[v2+256];
  254.     sin3:=sinustabel[v3];
  255.     cos3:=sinustabel[v3+256];
  256.  
  257.     v1:=(v1+2) AND 1023;
  258.     v2:=(v2-2) AND 1023;
  259.     v3:=(v3+1) AND 1023;
  260. end;
  261.  
  262. procedure RotateAllCoords; assembler;
  263. asm
  264.     mov    ax,ds
  265.     mov    es,ax
  266.     lea    si,coords
  267.     lea    di,cbuffer
  268.     mov    i,ANTAL_COORDS
  269.     cld
  270. @loop:
  271.     lodsw
  272.     mov    xkoord,ax
  273.     lodsw
  274.     mov    ykoord,ax
  275.     lodsw
  276.     mov    zkoord,ax
  277.  
  278.     mov    ax,xkoord               {rotate around Z-axis}
  279.     push    ax
  280.     imul    Cos1
  281.     add    ax,ax
  282.     adc    dx,dx
  283.     mov    bx,dx
  284.     mov    ax,ykoord
  285.     imul    Sin1
  286.     add    ax,ax
  287.     adc    dx,dx
  288.     sub    bx,dx
  289.     mov    xkoord,bx
  290.     pop    ax
  291.     imul    Sin1
  292.     add    ax,ax
  293.     adc    dx,dx
  294.     mov    bx,dx
  295.     mov    ax,ykoord
  296.     imul    Cos1
  297.     add    ax,ax
  298.     adc    dx,dx
  299.     add    bx,dx
  300.     mov    ykoord,bx
  301.  
  302.     mov    ax,ykoord               {rotate around Y-axis}
  303.     push    ax
  304.     imul    Cos2
  305.     add    ax,ax
  306.     adc    dx,dx
  307.     mov    bx,dx
  308.     mov    ax,zkoord
  309.     imul    Sin2
  310.     add    ax,ax
  311.     adc    dx,dx
  312.     sub    bx,dx
  313.     mov    ykoord,bx
  314.     pop    ax
  315.     imul    Sin2
  316.     add    ax,ax
  317.     adc    dx,dx
  318.     mov    bx,dx
  319.     mov    ax,zkoord
  320.     imul    Cos2
  321.     add    ax,ax
  322.     adc    dx,dx
  323.     add    bx,dx
  324.     mov    zkoord,bx
  325.  
  326.     mov    ax,xkoord               {rotate around X-axis}
  327.     push    ax
  328.     imul    Cos3
  329.     add    ax,ax
  330.     adc    dx,dx
  331.     mov    bx,dx
  332.     mov    ax,zkoord
  333.     imul    Sin3
  334.     add    ax,ax
  335.     adc    dx,dx
  336.     sub   bx,dx
  337.     mov    xkoord,bx
  338.     pop    ax
  339.     imul    Sin3
  340.     add    ax,ax
  341.     adc    dx,dx
  342.     mov    bx,dx
  343.     mov    ax,zkoord
  344.     imul    Cos3
  345.     add    ax,ax
  346.     adc    dx,dx
  347.     add    bx,dx
  348.     mov    zkoord,bx
  349.  
  350.     add    bx,800
  351.     and    bx,bx
  352.     jnz    @zero
  353.     mov    bl,1
  354. @zero:
  355.  
  356.     mov        ax,xkoord
  357.     cwd
  358.     mov        dl,ah
  359.     mov        ah,al
  360.     xor        al,al
  361.     idiv        bx
  362.     add        ax,80
  363.     stosw
  364.  
  365.     mov        ax,ykoord
  366.     cwd
  367.     mov        dl,ah
  368.     mov        ah,al
  369.     xor        al,al
  370.     idiv        bx
  371.     add        ax,50
  372.     stosw
  373.  
  374.     dec        i
  375.     jne        @loop
  376. end;
  377.  
  378.  
  379. function FaceShown(i : integer; l1,l2,l3 : byte) : boolean;
  380. var
  381.     a,b : longint;
  382. begin
  383.     a := (cbuffer[l1]-cbuffer[l2])*(cbuffer[l3+1]-cbuffer[l2+1]);
  384.     b := (cbuffer[l1+1]-cbuffer[l2+1])*(cbuffer[l3]-cbuffer[l2]);
  385.     light[i] := ((a-b) DIV 70)+1;
  386.     FaceShown := (a-b) > 0;
  387. end;
  388.  
  389.  
  390. procedure FillShape(anim : pAnim; y,ysize : integer; color : byte); assembler;
  391. const
  392.     PSIZE = ANIMWIDTH*ANIMHEIGHT;
  393.     planeadd : array[0..3] of word = (0,PSIZE,PSIZE*2,PSIZE*3);
  394. asm
  395.     mov    ax,y
  396.     add    ax,ax
  397.     mov    si,ax
  398.     les    di,anim
  399.     add    di,[si+OFFSET animytabel]
  400.     lea    si,slope
  401.     add    ax,ax
  402.     add    si,ax
  403.  
  404.     cld
  405. @yloop:
  406.     lodsw
  407.     mov    dx,ax
  408.     lodsw
  409.     cmp    ax,dx
  410.     jle    @exchange
  411.     xchg    ax,dx
  412. @exchange:
  413.     push    di
  414.  
  415.     mov    bx,ax
  416.     sub    dx,ax            {calc xsize in DX}
  417.     cmp    dx,0
  418.     jle    @drawn
  419.     cmp    dx,ANIMWIDTH*4
  420.     jge    @drawn
  421.     shr    ax,2            {calc xpos}
  422.     add    di,ax
  423.  
  424.     and    bx,3
  425.     add    bl,bl
  426.     add    di,WORD PTR [planeadd+bx]
  427.     shr    bl,1
  428.     mov    ah,4
  429.     sub    ah,bl
  430.  
  431.     mov    cx,dx
  432.     mov    dx,ANIMWIDTH*ANIMHEIGHT
  433.     mov    bx,(ANIMWIDTH*ANIMHEIGHT*4)-1
  434.     mov    al,color
  435. @xloop:
  436.     mov    es:[di],al
  437.     add    di,dx
  438.     dec    ah
  439.     jnz    @noswap
  440.     mov    ah,4
  441.     sub    di,bx
  442. @noswap:
  443.     inc    al
  444.     loop    @xloop
  445.  
  446. @drawn:
  447.     pop    di
  448.     add    di,ANIMWIDTH
  449.     dec    ysize
  450.     jnz    @yloop
  451. end;
  452.  
  453.  
  454. procedure PrintJellyLogo;
  455. var
  456.     i,pos : integer;
  457.     aptr : pAnim;
  458.     source_offset, dest_offset : word;
  459.     colorptr : pointer;
  460. begin
  461.     pos:=animpos;
  462.     source_offset:=0;                                    {start with 1. line...}
  463.     dest_offset:=20+(50*WIDTH)+display1;        {start pos on screen}
  464.     for i:=0 to ANIMHEIGHT-1 do begin
  465.         aptr:=@anim[pos]^;
  466.         asm
  467.             push    ds
  468.             cli
  469.             mov    dx,$3C4
  470.             mov    al,$02
  471.             out    dx,al
  472.             inc    dx
  473.             mov    al,$01
  474.             out    dx,al
  475.             sti
  476.             mov    es,SEGA000
  477.             mov    di,dest_offset
  478.             lds    si,aptr
  479.             add    si,source_offset
  480.             cld
  481.             mov    cx,ANIMWIDTH/2
  482.             rep movsw
  483.  
  484.             cli
  485.             mov    dx,$3C4
  486.             mov    al,$02
  487.             out    dx,al
  488.             inc    dx
  489.             mov    al,$02
  490.             out    dx,al
  491.             sti
  492.             mov    di,dest_offset
  493.             add    si,(ANIMWIDTH*ANIMHEIGHT)-ANIMWIDTH
  494.             mov    cx,ANIMWIDTH/2
  495.             rep movsw
  496.  
  497.             cli
  498.             mov    dx,$3C4
  499.             mov    al,$02
  500.             out    dx,al
  501.             inc    dx
  502.             mov    al,$04
  503.             out    dx,al
  504.             sti
  505.             mov    di,dest_offset
  506.             add    si,(ANIMWIDTH*ANIMHEIGHT)-ANIMWIDTH
  507.             mov    cx,ANIMWIDTH/2
  508.             rep movsw
  509.  
  510.             cli
  511.             mov    dx,$3C4
  512.             mov    al,$02
  513.             out    dx,al
  514.             inc    dx
  515.             mov    al,$08
  516.             out    dx,al
  517.             sti
  518.             mov    di,dest_offset
  519.             add    si,(ANIMWIDTH*ANIMHEIGHT)-ANIMWIDTH
  520.             mov    cx,ANIMWIDTH/2
  521.             rep movsw
  522.             pop    ds
  523.         end;
  524.         inc(source_offset,ANIMWIDTH);
  525.         inc(dest_offset,WIDTH);
  526.         inc(pos); if (pos > ANTAL_ANIMS) then pos:=0;
  527.     end;
  528. end;
  529.  
  530.  
  531. (*------------------------------------------------*)
  532.  
  533. procedure RunOnce;
  534. var
  535.     i : integer;
  536. begin
  537.     SwapDisplay;
  538.     while retraces=0 do ;
  539.     retraces:=0;
  540.     if DEBUG then SetRGB(0,30,0,0);
  541.  
  542.     ClearScreen(anim[animpos]);
  543.  
  544.     CalcVinkel;
  545.     RotateAllCoords;
  546.  
  547.     for i:=1 to ANTAL_FACES do begin
  548.         with face[i] do if FaceShown(i, l1 shl 1,l2 shl 1,l3 shl 1) then begin
  549.             ClearSlope;
  550.             miny := 200; maxy := 0;
  551.             CalcSlope(l1,l2);
  552.             CalcSlope(l2,l3);
  553.             CalcSlope(l3,l4);
  554.             CalcSlope(l4,l1);
  555.             FillShape(anim[animpos], miny, maxy-miny, light[i]);
  556.         end;
  557.     end;
  558.     PrintJellyLogo;
  559.     inc(animpos); if (animpos > ANTAL_ANIMS) then animpos:=0;
  560.     if DEBUG then SetRGB(0,0,0,0);
  561. end;
  562.  
  563.  
  564. begin
  565.     OpenScreen;
  566.     InitDemo;
  567.     SetAllInterrupts;
  568.     repeat RunOnce until KeyPressed;
  569.     RestoreAllInterrupts;
  570.     UninitDemo;
  571.     CloseScreen;
  572. end.
  573.